package com.example.testcalendaractivity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class EventFragment extends Fragment {
    private static final String ARG_EVENT_ID = "eventId"; // 唯一事件ID
    private static final String ARG_EVENT_TITLE = "eventTitle";
    private static final String ARG_EVENT_START_DATE = "eventStartDate";
    private static final String ARG_EVENT_END_DATE = "eventEndDate";
    private static final String ARG_EVENT_DESCRIPTION = "eventDescription";
    private static final String ARG_EVENT_LOCATION = "eventLocation";

    private long eventId;

    // 静态工厂方法
    public static EventFragment newInstance(String title, long startDate, long endDate, String description, String location, long eventId) {
        EventFragment fragment = new EventFragment();
        Bundle args = new Bundle();
        args.putLong(ARG_EVENT_ID,eventId);
        args.putString(ARG_EVENT_TITLE, title);
        args.putLong(ARG_EVENT_START_DATE, startDate);
        args.putLong(ARG_EVENT_END_DATE, endDate);
        args.putString(ARG_EVENT_DESCRIPTION, description);
        args.putString(ARG_EVENT_LOCATION, location);
        fragment.setArguments(args); //将数据传给fragment 以便后续生命周期使用
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_event, container, false); //LayoutInflater从指定的布局资源文件中加载一个视图（View）对象

        TextView eventTitle = view.findViewById(R.id.eventTitle);
        TextView eventDates1 = view.findViewById(R.id.eventDates1);
        TextView eventDates2 = view.findViewById(R.id.eventDates2);
        TextView eventDescription = view.findViewById(R.id.eventDescription);


        if (getArguments() != null) {
            eventId = getArguments().getLong(ARG_EVENT_ID);
            String title = getArguments().getString(ARG_EVENT_TITLE);
            long startDate = getArguments().getLong(ARG_EVENT_START_DATE);
            long endDate = getArguments().getLong(ARG_EVENT_END_DATE);
            String description = getArguments().getString(ARG_EVENT_DESCRIPTION);
            String location = getArguments().getString(ARG_EVENT_LOCATION);

            eventTitle.setText(title);

            eventDates1.setText("开始时间: " + formatDate(startDate));
            eventDates2.setText("结束时间: " + formatDate(endDate));

            if (description != null && !description.isEmpty()) {
                // 处理非空和非空字符串的逻辑
                eventDescription.setText(description);
            } else {
                eventDescription.setText("无事件描述");
                
            }

        }

        return view;
    }




    // 格式化日期为可读格式
    private String formatDate(long dateInMillis) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return formatter.format(new Date(dateInMillis));
    }
}

